/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.jsimplemarket.wicket.pages;

import com.googlecode.jsimplemarket.model.NStareSpatiuComercial;
import com.googlecode.jsimplemarket.model.NTipSpatiuComercial;
import com.googlecode.jsimplemarket.model.SpatiuComercial;
import com.googlecode.jsimplemarket.model.Rand;
import com.googlecode.jsimplemarket.service.GenericService;
import java.util.List;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Alexandru.Codescu
 */
public class SpatiuEditPage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<SpatiuComercial> service;
    @SpringBean(name = "genericService")
    private GenericService<NTipSpatiuComercial> serviceTipSpatiu;
    @SpringBean(name = "genericService")
    private GenericService<NStareSpatiuComercial> serviceStareSpatiu;
    
    @SpringBean(name = "genericService")
    private GenericService<Rand> serviceRand;
    
    private final SpatiuComercial entity;
    
    private void init() {
        Form<SpatiuComercial> form = new Form<SpatiuComercial>("form");
        form.setModel(new CompoundPropertyModel<SpatiuComercial>(entity));
        
        TextField<String> tfCod = new TextField<String>("cod");
        tfCod.setRequired(true);
        form.add(tfCod);
        
        DropDownChoice<NTipSpatiuComercial> ddcTipSpatiu = new DropDownChoice<NTipSpatiuComercial>("tipSpatiuComercial", new LoadableDetachableModel<List<NTipSpatiuComercial>>() {

            @Override
            protected List<NTipSpatiuComercial> load() {
                return serviceTipSpatiu.getList(NTipSpatiuComercial.class, null, null);
            }
            
        }, new ChoiceRenderer<NTipSpatiuComercial>("denumire", "id"));
        ddcTipSpatiu.setRequired(true);
        form.add(ddcTipSpatiu);
        
        DropDownChoice<NStareSpatiuComercial> ddcStareSpatiu = new DropDownChoice<NStareSpatiuComercial>("stareSpatiuComercial", new LoadableDetachableModel<List<NStareSpatiuComercial>>() {

            @Override
            protected List<NStareSpatiuComercial> load() {
                return serviceStareSpatiu.getList(NStareSpatiuComercial.class, null, null);
            }
            
        }, new ChoiceRenderer<NStareSpatiuComercial>("denumire", "id"));
        ddcStareSpatiu.setRequired(true);
        form.add(ddcStareSpatiu);
        
        
        DropDownChoice<Rand> ddcRand = new DropDownChoice<Rand>("rand", new LoadableDetachableModel<List<Rand>>() {

            @Override
            protected List<Rand> load() {
                return serviceRand.getList(Rand.class, null, null);
            }
            
        }, new ChoiceRenderer<Rand>("denumire", "id"));
        ddcRand.setRequired(true);
        form.add(ddcRand);
        
        
        form.add(new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (entity.getId() == null) {
                    service.insert(entity);
                } else {
                    service.update(entity);
                }
                setResponsePage(new SpatiuViewPage());
            }
            
        });
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(new SpatiuViewPage());
            }
            
        });
        
        add(form);
    }
    
    public SpatiuEditPage() {
        entity = new SpatiuComercial();
        init();
    }
    
    public SpatiuEditPage(Long id) {
        entity = service.getById(SpatiuComercial.class, id);
        init();
    }
}
